<?php

class CustomerController extends Controller
{
    /**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
    
    public function actionIndex()
    {
        $this->current_page = 'customer';
        
        $modelCreate = new Customer;
        
        if (isset($_POST['Customers']) && isset($_POST['hi-act-list-customers-form']))
        {
            if ($_POST['hi-act-list-customers-form'] == 'del') {
                foreach ($_POST['Customers'] as $customer_id)
                {
                    Customer::model()->deleteByPk($customer_id);
                }
            }
        }
        
        if (isset($_POST['Customer']) && isset($_POST['hi-act-add-customer-form']))
        {
            $modelCreate->attributes = $_POST['Customer'];
            if ($modelCreate->save())
                $modelCreate->unsetAttributes();
        }
        
        $dataCustomers = new CActiveDataProvider('Customer', array(
            'pagination'=>array(
                'pageSize'=>10,
            ),
        ));
        
        $this->render('index', array(
            'modelCreate' => $modelCreate,
            'dataCustomers' => $dataCustomers,
        ));
    }
    
    public function actionEdit($customer_id)
    {
        $modelUpdate = Customer::model()->findByPk($customer_id);
        
        if (isset($_POST['Customer']))
        {
            $modelUpdate = Customer::model()->findByPk($_POST['Customer']['id']);
            $modelUpdate->attributes = $_POST['Customer'];
            $modelUpdate->save();
            
            echo '<td align="center"><input checked type="checkbox" name="Customers[]" onclick="$(\'#hi-select-customer\').val(this.value);" value="'.$modelUpdate->id.'" onClick="//Check(document.myform.check_list)"></td>
                <td>'.$modelUpdate->name.'</td>
                <td>'.$modelUpdate->birthday.'</td> 
                <td>'.$modelUpdate->adress.'</td>
                <td>'.$modelUpdate->phone.'</td> 
                <td>'.$modelUpdate->visit.'</td>
                <td>'.$modelUpdate->recent.'</td>';
            
            Yii::app()->end();
        }
        
        $this->renderPartial('edit', array(
            'modelUpdate' => $modelUpdate,
        ));
    }
}